package org.csu.mypetstore.controller;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.tools.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping
    @ResponseBody
    public String get(String type, String username, String password){
        switch (type) {
            case "getAccountByUsernameAndPassword": {
                if (username == null || username.equals("")) {
                    //用户名为空
                    JSONObject object = new JSONObject();
                    object.put("status", "NOK");
                    object.put("msg", "用户名不允许为空!");
                    System.out.println(object);
                    return object.toString();
                } else if (password == null || password.equals("")) {
                    //密码为空
                    JSONObject object = new JSONObject();
                    object.put("status", "NOK");
                    object.put("msg", "密码不允许为空!");
                    System.out.println(object);
                    return object.toString();
                }
                password = MD5.getMD5(password);
                //用户名密码都正确匹配
                Account account = accountService.getAccountByUsernameAndPassword(username, password);
                JSONObject object = new JSONObject();
                if (account == null) {
                    //没有用户
                    object.put("status", "NOK");
                    object.put("msg", "用户不存在或者密码错误!");
                } else if (accountService.isAdmin(username)) {
                    //管理员
                    object.put("status", "OK");
                    object.put("msg", "admin");
                    object.put("data", account);
                } else {
                    //用户
                    object.put("status", "OK");
                    object.put("msg", "登录成功!");
                    object.put("data", account);
                }
                System.out.println(object);
                return object.toString();
            }
            case "getAccountByUsername": {
                if (username == null || username.equals("")) {
                    //用户名为空
                    JSONObject object = new JSONObject();
                    object.put("status", "NOK");
                    object.put("msg", "用户名不允许为空");
                    System.out.println(object);
                    return object.toString();
                }
                Account account = accountService.getAccountByUsername(username);
                JSONObject object = new JSONObject();
                if (account == null) {
                    //没有用户
                    object.put("status", "NOK");
                    object.put("msg", "用户不存在!");
                } else {
                    object.put("status", "OK");
                    object.put("msg", "查询成功!");
                    object.put("data", account);
                }
                System.out.println(object);
                return object.toString();
            }
            case "getAccountList": {
                List<Account> accounts = accountService.getAccountList();
                JSONObject object = new JSONObject();
                object.put("status", "OK");
                object.put("msg", "用户列表查询成功!");
                object.put("data", accounts);
                return object.toString();
            }
        }
        JSONObject object = new JSONObject();
        object.put("status", "NOK");
        object.put("msg", "出现未知异常,请刷新重试!");
        return object.toString();
    }

    @PostMapping
    @ResponseBody
    public String add(Account account){
        //还需加入判断是否参数正确
        JSONObject object = new JSONObject();
        account.setPassword(MD5.getMD5(account.getPassword()));
        String s = accountService.addAccount(account);
        if(s.equals("Username already exists")){
            object.put("status", "NOK");
            s = "用户名已存在,请更换用户名!";
        }else if(s.equals("Succeed in adding account and signon")){
            object.put("status", "OK");
            s = "用户注册成功!";
        }else{
            object.put("status", "NOK");
            s = "用户注册失败!";
        }
        object.put("msg", s);
        return object.toString();
    }

    @PutMapping
    @ResponseBody
    public String update(String type, Account account){
        System.out.println(type);
        System.out.println(account.getUsername());
        account.setPassword(MD5.getMD5(account.getPassword()));
        if(type.equals("updateAccount")){
            JSONObject object = new JSONObject();
            String s =  accountService.updateAccount(account);
            if(s.equals("Succeed in updating account")){
                object.put("status", "OK");
                s = "用户信息更新成功!";
            }else {
                object.put("status", "NOK");
                s = "用户信息更新失败!";
            }
            object.put("msg", s);
            return object.toString();
        }
        else if(type.equals("updateSignon")){
            JSONObject object = new JSONObject();
            String username = account.getUsername();
            String password = account.getPassword();
            String s = accountService.updateSignon(username, password);
            if(s.equals("Succeed in updating signon")){
                object.put("status", "OK");
                s = "用户密码更新成功!";
            }else {
                object.put("status", "NOK");
                s = "用户密码更新失败!";
            }
            object.put("msg", s);
            return object.toString();
        }
        JSONObject object = new JSONObject();
        object.put("status", "NOK");
        object.put("msg", "出现未知异常,请刷新重试!");
        return object.toString();
    }

    @DeleteMapping
    @ResponseBody
    public String delete(String username){
        String s = accountService.deleteAccount(username);
        JSONObject object = new JSONObject();
        if(s.equals("Username does not exist")){
            //用户名不存在
            object.put("status", "NOK");
            s = "该用户不存在!";
        }else if(s.equals("Succeed in deleting account and signon")){
            object.put("status", "OK");
            s = "删除用户成功!";
        }else{
            object.put("status", "NOK");
            s = "出现未知异常，请刷新重试!";
        }
        object.put("msg", s);
        return object.toString();
    }

}
